<template>
  <d2-container>

    <template slot="header">
      <el-form :gutter="24" ref="search-box" :model="mainQuery"  label-position="left" :inline="true">
        <el-form-item label="单号：" size="mini">
          <el-input placeholder="" v-model="mainQuery.tradeNo" @keyup.enter.native="handleSearch" size="small" clearable></el-input>
        </el-form-item>

        <el-form-item label="商户号：" size="mini">
          <el-input placeholder="" v-model="mainQuery.mchNo" @keyup.enter.native="handleSearch" size="small" clearable></el-input>
        </el-form-item>

        

        <el-form-item label="网站：" size="mini">
          <el-input placeholder="" v-model="mainQuery.payWebSite" @keyup.enter.native="handleSearch" size="small" clearable></el-input>
        </el-form-item>

        <el-form-item label="类型：" size="mini">
          <el-select v-model="mainQuery.type" clearable>
            <el-option
              v-for="item in typeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        </el-form-item>

        <el-form-item label="时间区间：" size="mini">
          <el-date-picker
            size="small" 
            v-model="dateTimeRange"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss"
            :picker-options="datetimePickerOptions"
            @change="onDateTimePickerChange"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
      </el-form>

      <div class="search-btn-box">
        <div class="right">
          <el-button class="filter-item" icon="el-icon-download" size="small" @click="handleExportExcel">导出</el-button>
          <el-button class="filter-item" icon="" type="default" @click="resetMainData" :loading="mainTable.loading" size="small">重置</el-button>
          <el-button class="filter-item" icon="el-icon-search" type="primary" @click="handleSearch" :loading="mainTable.loading" size="small">查询</el-button>
        </div>
      </div>
    </template>

    <!--主列表数据-->
    <d2-crud
      ref="d2Crud"
      :options="mainTable.options"
      :index-row="mainTable.indexRow"
      :columns="mainTable.columns"
      :data="mainTable.list"
      :selection-row="mainTable.allowSelect"
      :loading="mainTable.loading"
      @current-change="onMainCurrentChange"
      @selection-change=""
    ></d2-crud>

    <template slot="footer">
      <el-pagination
        @size-change="handleMainSizeChange"
        @current-change="handleMainPageChange"
        :current-page="mainTable.pagination.currentPage"
        :page-sizes="mainTable.pagination.pageSizes"
        :page-size="mainTable.pagination.pageSize"
        :page-count="mainTable.pagination.pageCount"
        :total="mainTable.pagination.total"
        layout="total, sizes, prev, pager, next, jumper"
        :background="true"
        >
      </el-pagination>
    </template>

    <common-export ref="common-export"></common-export>

  </d2-container>
</template>

<script>

import ApiMain from '@/api/risk.tradeOrder';
import CommonExport from '../../common/export';
import util from '@/libs/util.js';
import DatetimePickerMixin from '@/mixin/DatetimePickerMixin'


export default {
  name: 'risk-tradeOrder',
  mixins: [
    DatetimePickerMixin,
  ],
  components: {
    'common-export': CommonExport,
  },
  data () {
    return {

      mainQuery: {
        tradeNo:  undefined,
        merNo:  undefined,
        terNo:  undefined,
        type:  undefined,
        payWebSite:  undefined,
        startTime:  undefined,
        endTime:undefined,
      },

      mainTable: {
        loading: false,
        options: {
          border             : true,
          stripe             : true,
          highlightCurrentRow: true,
          emptyText          : '暂无数据',
          size               : 'mini',
        },
        // indexRow: {
        //   title:'序号',
        //   width: "80",
        //   align:'center'
        // },
        indexRow: null,
        allowSelect: false,
        columns: [
          {title: '单号', key: 'tradeNo', align: 'center', width: 200, showOverflowTooltip: true, fixed: true},
          {title: '商户号', key: 'mchNo', align: 'center', width: 180, showOverflowTooltip: true},
          {title: '网址', key: 'payWebSite', align: 'center', width: 200, showOverflowTooltip: true},
          {title: '内容', key: 'info', align: 'center', width: 280, showOverflowTooltip: true},
          {title: '类型', key: 'type', align: 'center', showOverflowTooltip: true, formatter (row, column, cellValue, index) {
            if (cellValue == 1) {
              return '风控订单'
            } else {
              return '可疑风控订单'
            }
          }},
          {title: '阻挡时间', key: 'createTime', align: 'center', width: 200, showOverflowTooltip: true},
          {title: '处理方式', key: 'action', align: 'center', showOverflowTooltip: true, formatter (row, column, cellValue, index) {
            if (cellValue == 1) {
              return '拒绝交易'
            } else if (cellValue == 2) {
              return '接受交易'
            } else if (cellValue == 3) {
              return '审核'
            }
          }},
        ],
        list: [],
        pagination: {
          currentPage: 1,
          pageSize   : 50,
          total      : 0,
          pageSizes  : [50, 100, 200]
        },
      },

      dateTimeRange: [],
      typeOptions: [
        {label: '风控订单', value: 1},
        {label: '可疑订单', value: 2}
      ]
    }
  },
  mounted () {
    this.resetMainData();
  },
  beforeDestroy () {
  },
  methods: {

    handleSearch () {
      this.mainTable.pagination.currentPage = 1;
      this.fetchMainData();
    },

    renderMainData (re) {
      this.mainTable.list = re.list;
      this.mainTable.pagination.total = re.total;
    },

    handleMainSizeChange (val) {
      this.mainTable.pagination.pageSize = val;
      this.mainTable.pagination.currentPage = 1;
      this.fetchMainData()
    },
    handleMainPageChange (val) {
      this.mainTable.pagination.currentPage = val;
      this.fetchMainData()
    },

    buildMainQuery () {

      var query = {
        page  : this.mainTable.pagination.currentPage,
        limit : this.mainTable.pagination.pageSize,
        search: this.mainQuery,
      };

      return query;
    },

    resetMainData () {

      this.mainQuery = {
        tradeNo:  undefined,
        merNo:  undefined,
        terNo:  undefined,
        type:  undefined,
        payWebSite:  undefined,
        startTime:  undefined,
        endTime:undefined,
      };

      this.fetchMainData();
    },

    async fetchMainData () {

      this.mainTable.loading = true;

      try {
        var re = await ApiMain.list(this.buildMainQuery());
        this.renderMainData(re);
      } catch (err) {}

      this.mainTable.loading = false;

    },

    onDateTimePickerChange() {
      this.mainQuery.startTime = this.dateTimeRange && this.dateTimeRange.length > 0 ? this.dateTimeRange[0] : ''
      this.mainQuery.endTime = this.dateTimeRange && this.dateTimeRange.length > 0 ? this.dateTimeRange[1] : ''
    },

    onMainCurrentChange (now, old) {
      this.mainTable.current = now;
    },

    onMainDoubleClick (now) {
      this.mainTable.current = now;
      this.handleSet();
    },

    handleAdd () {
      this.$refs['dialog-add'].show();
    },

    handleSet () {
      if (this.mainTable.current) {
        this.$refs['dialog-add'].show(this.mainTable.current.id);
      }
    },

    handleExportExcel () {
      this.$refs['common-export'].handleExportExcel(ApiMain, this.mainQuery);
    },

  }
}
</script>

<style>

</style>
